var Form = (function() {
    /**
     * 适用于bootstrap的校验规则替换
     * @returns {{errorElement: string, highlight: Function, unhighlight: Function, success: Function}}
     */
    function getDefaultOptions() {
        var ID = /\d{17}[\dXx]/;

        $.validator.addMethod('IDCARD', function(value, ele) {
            return ID.test(value);
        }, '身份证号格式错误');

        var BANK = /[0-9]{12,}/;
        $.validator.addMethod('BANK', function(value, ele) {
            return BANK.test(value);
        }, '银行卡号码错误');

        return {errorElement: 'span'}
    }

    return {
        //注册
        register: function($form) {
            var _options = getDefaultOptions();
            $.extend(_options, {
                rules: {
                    //idcard: {IDCARD: true},
                    //tel: {minlength: 11},
                    password: {minlength: 6},
                    password1: {equalTo: '#password'}
                }
            });
            $form.validate(_options);
        },

        //重置密码
        forget: function($form, success) {
            var _options = getDefaultOptions();
            $.extend(_options, {
                rules: {
                    idcard: {IDCARD: true},
                    password: {minlength: 6},
                    password1: {equalTo: '#password'}
                },
                submitHandler: function() {
                    $.post($form.attr('action'), $form.serialize(), function(data) {
                        data = JSON.parse(data);
                        if(data.status == 0) {
                            alert(data.msg);
                        } else {
                            success();
                        }
                    });
                }
            });

            $form.validate(_options);
        },

        modify: function($form, submit) {
            var _options = getDefaultOptions();
            $.extend(_options, {
                rules: {
                    idcard: {IDCARD: true},
                    bankaccount: {BANK: true},
                    tel: {minlength: 11}
                },
                submitHandler: submit
            });

            $form.validate(_options);
        }
    }

})();

(function($) {
    $.fn.WxUpload = function(callback) {
        function uploadImg (res) {
            wx.uploadImage({
                localId: res.localIds[0],
                isShowProgressTips: 1,
                success: function (res) {
                    var serverId = res.serverId;
                    callback(serverId);
                }
            });
        }

        function chooseImg () {
            wx.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success: uploadImg
            });
        }

        this.click(chooseImg);
    }
})(jQuery);